package fotoboek.gui;

import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;

public class Button {
    public-init var button: Group on replace{
        _normal = button.content[4];
        _hovered = button.content[3];
        _pressed = button.content[2];
        _disabled = button.content[1];
        _activeArea = button.content[0];
        _text = button.content[5] as Text;
        _text.textOrigin = TextOrigin.BASELINE;        
    }
    public-init var _normal: Node;
    public-init var _hovered: Node;
    public-init var _pressed: Node;
    public-init var _disabled: Node;
    public-init var _activeArea: Node;
    public-init var _text: Text;
    public-init var rightPadding = 0;

    public var action: function():Void;

    public var text: String on replace {
        if (text != null) {
            _text.content = text;
            align(_normal, _text);
        }
    }


    public var disable = false on replace{
        _activeArea.disable = disable;
        if (disable){
            isPressed =
            isHovered = false;
            _normal.visible =
            _hovered.visible =
            _pressed.visible = false;
        }
        else {
            _normal.visible =
            _hovered.visible =
            _pressed.visible = true;
        }
    }
    public var isPressed: Boolean on replace{
        updateUI();
    }
    public var isHovered: Boolean on replace{
        updateUI();
    }
    init{
        if(_activeArea != null){
            _activeArea.onMouseEntered = function (e){
                if(not disable){
                    isHovered = true;
                }
            }
            _activeArea.onMouseExited = function (e){
                if(not disable){
                    isHovered = false;
                }
            }
            _activeArea.onMousePressed = function (e){
                if(not disable){
                    isPressed = true;
                }
            }
            _activeArea.onMouseReleased = function (e){
                if(not disable){
                    isPressed = false;
                    action();
                }
            }
        }
        isPressed = false;
        isHovered = false;
        updateUI();
    }

   public function updateUI(){
        if (_pressed != null)
        _pressed.visible = isPressed;
        if (_hovered != null)
        _hovered.visible = not isPressed and isHovered;
        if (_hovered != null)
        _normal.visible = not isPressed and not isHovered;
    }

    function align( target:Node, dest:Text ) {
        def destBounds = dest.boundsInLocal;
        def targetBounds = target.boundsInLocal;

        //println("{destBounds.minX}, {destBounds.minY}|{destBounds.width}x{destBounds.height}");
        // Delete unexpected Affine Transform created during translation by Production Suite
        delete dest.transforms;
        dest.translateY = 0;
        dest.translateX = 0;
        // center X
        dest.x =  rightPadding + (targetBounds.width - destBounds.width) / 2;
        //println("target {targetBounds.minX}, {targetBounds.minY}|{targetBounds.width}x{targetBounds.height}");
        dest.y = destBounds.height + (targetBounds.height - destBounds.height) / 2;

    }
}